#include <iostream>
#include <cstdlib>
#include <omp.h>

void init(float *v, int N)
{
#pragma omp parallel for
    for (int i = 0; i < N; ++i)
        v[i] = static_cast<float>(i + 1);
}

void output(const float *p, int N)
{
    std::cout << "First 5 results: ";
    for (int i = 0; i < 5 && i < N; ++i)
        std::cout << p[i] << " ";
    std::cout << "\n";
}

void vec_mult(int N)
{
    float *v1 = new float[N];
    float *v2 = new float[N];
    float *p = new float[N];

#pragma omp parallel num_threads(2)
    {
#pragma omp single
        {
#pragma omp task depend(out : v1[0 : N])
            init(v1, N);

#pragma omp task depend(out : v2[0 : N])
            init(v2, N);

// **移除了nowait子句**
#pragma omp task depend(in : v1[0 : N], v2[0 : N]) depend(out : p[0 : N])
            {
#pragma omp parallel for
                for (int i = 0; i < N; ++i)
                    p[i] = v1[i] * v2[i];
            }

#pragma omp task depend(in : p[0 : N])
            output(p, N);
        }
    }

    delete[] v1;
    delete[] v2;
    delete[] p;
}

int main()
{
    const int N = 100;
    vec_mult(N);
    return 0;
}